<?php
/**
 * ClassName:excel导入处理器
 * @auditor:leijunxiang
 * @time 2025/4/8 16:13
 */

namespace app\tool\word;

use app\model\word\TableData;
use app\tool\ToolBase;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Log;

class ExcelInputHandle extends ToolBase
{
    //执行控制对象
    protected $dataControllerObj;
    //标题处理对象
    protected $titleHandleObj;
    //参数处理对象
    protected $paramHandleObj;
    //文件路径
    protected $file;
    //表格数据对象
    protected $tableDataObj;


    /**
     * 开始导入(文件,货代id,渠道id,开始标识,行结束位移)
     */
    public function startInput($file, $forwardId, $startFlag, $rowOffset)
    {
        if ($file && $forwardId && $startFlag && $rowOffset) {
            //初始化数据
            $this->initData($file);
            //校验文件
            $this->verifyFile();
            //加载文件
            $worksheets = $this->loadFile();
            //处理表格数据
            $this->handleExcelData($worksheets, $forwardId, $startFlag, $rowOffset);
            //清楚数据
            $this->cleanData();
        } else {
            $this->errCode = FAIL;
            $this->errMsg = "文件路径为空";
        }
        return $this->isGoOn();
    }

    /**
     * 初始化数据
     */
    protected function initData($file)
    {
        $this->init();
        //执行控制对象
        $this->dataControllerObj = null;
        //标题处理对象
        $this->titleHandleObj = null;
        //参数处理对象
        $this->paramHandleObj = null;
        //文件
        $this->file = $file;
        //表格数据对象
        if (empty($this->tableDataObj)) {
            $this->tableDataObj = app(TableData::class);
        }

    }

    /**
     * 校验文件
     */
    protected function verifyFile()
    {
        if (!$this->isGoOn()) {
            return 0;
        }
//        $allowedExtensions = ['xls', 'xlsx'];
//        $fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION);
//
//        if (!in_array(strtolower($fileExtension), $allowedExtensions)) {
//            $this->errCode = FAIL;
//            $this->errMsg = "文件格式不正确，支持的格式为: " . implode(', ', $allowedExtensions);
//        }
    }


    /**
     * 加载文件
     */
    protected function loadFile()
    {
        if (!$this->isGoOn()) {
            return 0;
        }
        // 加载文件
        $spreadsheet = IOFactory::load($this->file);
        // 获取所有工作表
        $worksheets = $spreadsheet->getAllSheets();
        if (empty($worksheets)) {
            $this->errMsg = "未找到数据";
            $this->errCode = FAIL;
            return "";
        }
        return $worksheets;
    }


    /**
     * 处理表格数据
     */
    protected function handleExcelData($worksheets, $forwardId, $startFlag, $rowOffset)
    {
        if (!$this->isGoOn()) {
            return;
        }
        $tool = app(WordHandleTool::class);
        $handle = app(ExecuteControllerHandle::class);
        foreach ($worksheets as $index => $worksheet) {
            // 获取最高行和列
            $highestRow = $worksheet->getHighestRow();
            $highestColumn = $worksheet->getHighestColumn();
            // 遍历每一行
            for ($row = 1; $row <= $highestRow; ++$row) {
                // 遍历每一列
                for ($col = 'A'; $col <= $highestColumn; ++$col) {
                    $cellValue = $tool->startHandle($worksheet, $row, $col);
                    // 获取单元格的值
//                    $cellValue = $worksheet->getCell($col . $row)->getValue();
                    if (gettype($cellValue) == "object") {
                        continue;
                    }
                    //(开始数组)开始执行(行，列，数据，excel对象,表格数据对象,货代id,开始标识,行结束位移)
                    $flag = $handle->startExcel($row, $col, $cellValue, $worksheet,$this->tableDataObj, $forwardId, $startFlag, $rowOffset);
                    if (!$flag) {
                        if ($handle->getErrCode() == WAIT) {
                            Log::info($handle->getErrMsg());
                        }
                        if ($handle->getErrCode() == FAIL) {
                            $this->errCode = $handle->getErrCode();
                            $this->errMsg = $handle->getErrMsg();
                            return;
                        }
                    }
                }
            }

        }
        if(!$this->tableDataObj->getIsSaveTrue()){
                $this->errCode = FAIL;
                $this->errMsg = "数据导入失败,未找到标识";
        }
    }

    /**
     * 清楚数据
     */
    protected function cleanData()
    {
        //执行控制对象
        $this->dataControllerObj = null;
        //标题处理对象
        $this->titleHandleObj = null;
        //参数处理对象
        $this->paramHandleObj = null;
        //文件路径
        $this->filePath = "";
        //表格数据对象
        $this->tableDataObj = null;

    }


}